package com.exam.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.controller.dto.PaperDTO;
import com.exam.demo.entity.SelectCourse;
import com.exam.demo.service.SelectCourseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/selectCourse")
public class SelectCourseController {
    @Resource
    SelectCourseService selectCourseService;

    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize,
                       @RequestParam(defaultValue = "") String studentName){
        System.out.println("111111111111111111111111");
        Page<SelectCourse> page = selectCourseService.findPage(new Page<>(pageNum,pageSize),studentName);
        return Result.success(page);
    }

    //查找考试信息
    @GetMapping("/exam")
    public Result exam(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String paperName,
                           @RequestParam(defaultValue = "") String studentId,
                           @RequestParam(defaultValue = "2") Integer examType
    ){
        System.out.println("studentId = " + studentId);
        Page<PaperDTO> page = selectCourseService.exam(new Page<>(pageNum,pageSize),paperName,studentId,examType);
        return Result.success(page);
    }

    //查找全部选课信息
    @GetMapping("/findAll")
    public Result findPage(){
        return Result.success(selectCourseService.list());
    }

    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody SelectCourse selectCourse){
        return Result.success(selectCourseService.saveOrUpdate(selectCourse));
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(selectCourseService.removeById(id));
    }

    /**
     * excel 导入
     *
     * @param file
     * @throws Exception
     */
    @PostMapping("/import")
    public Result imp(MultipartFile file) throws Exception {
        System.out.println("开始导入");
        boolean b = selectCourseService.imp(file);
        return Result.success(b); //false导入失败
    }

    /**
     * 导出接口
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        response = selectCourseService.export(response);
    }
}
